﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class AdminUserService
    {
        //添加用户
        //编辑用户
        //删除用户
        //查询用户列表
        //查询单个用户信息
        public static Model.AdminUser SelectUserByID(int id)
        {
            string sql = string.Format("SELECT * FROM AdminUser WHERE id = {0}",id);
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
            Model.AdminUser u = new Model.AdminUser();
            if (dr.HasRows)
            {
                dr.Read();
                u.id = int.Parse(dr[0].ToString());
                u.username = dr[1].ToString();
                u.pwd = dr[2].ToString();
                u.name = dr[3].ToString();
                dr.Close();
            }
            dr.Close();
            return u;
        }
        public static Model.AdminUser SelectUserByUsername(string username)
        {
            string sql = string.Format("SELECT * FROM AdminUser WHERE username = '{0}'", username);
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
            Model.AdminUser u = new Model.AdminUser();
            if (dr.HasRows)
            {
                dr.Read();
                u.id = int.Parse(dr[0].ToString());
                u.username = dr[1].ToString();
                u.pwd = dr[2].ToString();
                u.name = dr[3].ToString();
                dr.Close();
            }
            dr.Close();
            return u;
        }
        //用户登陆
        public static bool UserLogin(string username, string pwd)
        {
            string sql = string.Format("SELECT * FROM AdminUser WHERE username = '{0}' AND pwd = '{1}'",username,pwd);
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
            if (dr.HasRows)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
